<template>
    <div class="goods-relevant">
        <div class="header">
            <i class="icon"/>
            <span class="title">{{goodsId?'同类商品推荐':'猜你喜欢'}}</span>
        </div>

        <el-carousel :autoplay="false" height="350px" :interval="5000">
            <el-carousel-item v-for="i in Math.ceil(sliders.length / 4)" :key="i">
                <div class="slider">
                    <RouterLink v-for="goods in sliders.slice((i-1)*4,i*4)" :key="goods.id"
                                :to="`/product/${goods.id}`">
                        <img :src="goods.picture" alt="">
                        <p class="name ellipsis">{{goods.name}}</p>
                        <p class="price">&yen;{{goods.price}}</p>
                    </RouterLink>
                </div>
            </el-carousel-item>
        </el-carousel>

    </div>
</template>

<script>
    import {findRelevantGoods} from "@/api/product";

    export default {
        name: "GoodsRelevant",
        props: {
            goodsId: {
                type: String,
                default: ''
            }
        },
        data() {
            return {
                sliders: []
            }
        },
        created() {
            this.findRelevantGoods()
        },
        methods: {
            async findRelevantGoods() {
                const {result} = await findRelevantGoods({id: this.goodsId})
                this.sliders = result
            }
        }
    }
</script>

<style lang="less">

    .goods-relevant {
        background: #fff;
        min-height: 460px;
        margin-top: 20px;

        .header {
            height: 80px;
            line-height: 80px;
            padding: 0 20px;

            .title {
                font-size: 20px;
                padding-left: 10px;
            }

            .icon {
                width: 16px;
                height: 16px;
                display: inline-block;
                border-top: 4px solid @xtxColor;
                border-right: 4px solid @xtxColor;
                box-sizing: border-box;
                transform: rotate(45deg);

                &::before {
                    content: "";
                    width: 10px;
                    height: 10px;
                    position: absolute;
                    left: 0;
                    top: 2px;
                    background: lighten(@xtxColor, 40%);
                }
            }
        }

        .slider {
            display: flex;
            justify-content: space-around;
            padding: 0 40px;

            > a {
                width: 240px;
                text-align: center;

                img {
                    width: 230px;
                    height: 230px;
                    padding: 20px;
                }
                .name {
                    font-size: 16px;
                    color: #666;
                    padding: 0 40px;
                }
                .price {
                    font-size: 16px;
                    color: @priceColor;
                    margin-top: 15px;
                }
            }

        }
        .el-carousel__button{
            background: @xtxColor;
            width: 10px;
            height: 10px;
            border-radius: 5px;
        }

        .el-carousel__item:nth-child(2n) {

        }

        .el-carousel__item:nth-child(2n+1) {

        }
        .el-carousel__arrow--left{
            left: 20px;
        }
    }


</style>